package com.demo2do.fms.admin.web.controller.data.cost;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.report.ExportType;
import com.demo2do.fms.admin.entity.report.data.OilPriceReportSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.data.OilPriceService;
import com.demo2do.fms.admin.utils.FormatUtils;
import com.demo2do.fms.admin.visitor.impl.PersistentVisitor;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.data.cost.OilPrice;

/**
 * 油价管理
 * 
 * @author Chang Liu
 */
@Controller
@MenuSetting("menu-data")
@RequestMapping("/oil-prices")
public class OilPriceController {
    
    @Autowired
    private OilPriceService oilPriceService;
    
    @Autowired
    private PersistentVisitor persistentVisitor;
    
    /**
     * 
     * @param orgIds
     * @param page
     * @return
     */
    @MenuSetting("submenu-oil-price")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView list(@Secure(property = "orgIds") List<Long> orgIds, Page page){
        
        ModelAndView modelAndView = new ModelAndView("data/cost/oil-price/oil-price-list");
        
        int totals = oilPriceService.count(orgIds);
        if(totals > 0){
            
            List<OilPrice> oilPrices = oilPriceService.list(orgIds, page.with(totals));
            
            modelAndView.addObject("oilPrices", oilPrices);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param orgIds
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search(@Secure(property = "orgIds") List<Long> orgIds){
        
        ModelAndView modelAndView = new ModelAndView("data/cost/oil-price/oil-price-search");
        
        modelAndView.addObject("orgIds", orgIds);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param organizationIds
     * @param page
     * @param request
     * @return
     */
    @MenuSetting("submenu-oil-price")
    @RequestMapping(value = "", method = RequestMethod.GET, params= "filter=true")
    public ModelAndView onSearch(@RequestParam(value = "organizationIds") List<Long> organizationIds, 
                                 Page page, HttpServletRequest request){
        
        ModelAndView modelAndView = new ModelAndView("data/cost/oil-price/oil-price-list");
        
        int totals = oilPriceService.count(organizationIds);
        if(totals > 0) {
            
            List<OilPrice> oilPrices = oilPriceService.list(organizationIds, page.with(totals));
            
            modelAndView.addObject("oilPrices", oilPrices);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 选择公司和油类型 load出现最新的油价及时间
     * 
     * @param orgId
     * @param oilType
     * @return
     */
    @MenuSetting("submenu-oil-price")
    @RequestMapping(value = "", method = RequestMethod.GET, params= "prepare" , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onPrepare(@RequestParam(value = "orgId", required=false) Long orgId, 
                                              @RequestParam(value = "oilType", required=false) Long oilType){
        
        HttpResult httpResult = new HttpResult();
        
        OilPrice oilPrice = oilPriceService.getOilPrice(orgId, oilType);
        
        httpResult.success().data("oilPrice" , oilPrice);
            
        return httpResult;
    }
    
    /**
     * display create panel
     * 
     * @param orgIds
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create(@Secure(property = "orgIds") List<Long> orgIds){
        
        ModelAndView modelAndView = new ModelAndView("data/cost/oil-price/oil-price-create");
        
        modelAndView.addObject("orgIds", orgIds);
        
        return modelAndView;
    }
    
    /**
     * on create
     * 
     * @param login
     * @param oilPrice
     * @return
     */
    @MenuSetting("submenu-oil-price")
    @RequestMapping(value = "", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreate(@Secure Login login, OilPrice oilPrice){
        
        HttpResult httpResult = new HttpResult();
        
        oilPriceService.create(login.getId(),oilPrice);
        
        httpResult.success().message("设置成功");
        
        return httpResult;
        
    }
    
    /**
     * 输出当前页记录
     * 
     * @param organizationIds
     * @param orgIds
     * @param page
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export(@RequestParam(name = "organizationIds", required = false) List<Long> organizationIds,
                               @Secure(property = "orgIds") List<Long> orgIds,
                               Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("data/cost/oil-price-export.xlsx"));
        
        // 1. list oil pricre
        List<Long> companyIs = CollectionUtils.isNotEmpty(organizationIds) ? organizationIds : orgIds;
        int totals = oilPriceService.count(companyIs);
        if(totals > 0) {
            List<OilPrice> oilPrices = oilPriceService.list(companyIs, page.with(totals));
            modelAndView.addObject("oilPrices", oilPrices);
        }
        
        // 2. persist
        modelAndView.addObject("persist", persistentVisitor);
        modelAndView.addObject("type", ExportType.CURRENT);
        
        // 3. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
    /**
     * 输出所有记录
     * 
     * @param orgIds
     * @param search
     * @return
     */
    @RequestMapping(value = "/export-all", method = RequestMethod.GET)
    public ModelAndView exportAll(@Secure(property = "orgIds") List<Long> orgIds, OilPriceReportSearch search) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("data/cost/oil-price-export.xlsx"));
        
        // 1. list result
        if (CollectionUtils.isEmpty(search.getOrganizationIds())) {
            search.setOrganizationIds(orgIds);
        }
        List<Map<String, Object>> results = oilPriceService.exportAll(search);
        modelAndView.addObject("results", results);
        
        // 2. attribute
        modelAndView.addObject("type", ExportType.ALL);
        
        // 3. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }

    
}
